package jp.co.apl.haven_enquete.android.dao;

import java.util.List;
import java.util.Map;

import jp.co.apl.haven.generated.HavenProtos.Answer;
import jp.co.apl.haven_enquete.android.dao.AnswerDataDao;
import jp.co.apl.haven_enquete.android.db.DatabaseHelper;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
import android.util.Log;

public class AnswerDataDaoTest extends AndroidTestCase {

    private AnswerDataDao target;
    private DatabaseHelper databaseHelper;

    protected void setUp() throws Exception {
        super.setUp();
        target = new AnswerDataDao(mContext);
        databaseHelper = new DatabaseHelper(mContext);
    }

    public void testLoad() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        try {
            db.execSQL("delete from ANSWER");
            db.execSQL("insert into ANSWER (_id, value) values (1, 'A1')");
            db.execSQL("insert into ANSWER (_id, value) values (2, 'A2')");
            db.execSQL("insert into ANSWER (_id, value) values (3, 'A3')");
            db.execSQL("insert into ANSWER (_id, value) values (4, 'A4')");
            db.execSQL("insert into ANSWER (_id, value) values (5, 'A5')");
        } finally {
            db.close();
        }

        List<Answer> list = target.findAll();

        Log.i("haven", "sim serial = " + list.get(0).getTerminalId());

        assertEquals(5, list.size());
        assertEquals(1, list.get(0).getQuistionId());
        assertEquals(2, list.get(1).getQuistionId());
        assertEquals(3, list.get(2).getQuistionId());
        assertEquals(4, list.get(3).getQuistionId());
        assertEquals(5, list.get(4).getQuistionId());
        assertEquals("A1", list.get(0).getAnswer());
        assertEquals("A2", list.get(1).getAnswer());
        assertEquals("A3", list.get(2).getAnswer());
        assertEquals("A4", list.get(3).getAnswer());
        assertEquals("A5", list.get(4).getAnswer());
    }

    public void testLoadAsMap() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        try {
            db.execSQL("delete from ANSWER");
            db.execSQL("insert into ANSWER (_id, value) values (1, 'A1')");
            db.execSQL("insert into ANSWER (_id, value) values (2, 'A2')");
            db.execSQL("insert into ANSWER (_id, value) values (3, 'A3')");
            db.execSQL("insert into ANSWER (_id, value) values (4, 'A4')");
            db.execSQL("insert into ANSWER (_id, value) values (5, 'A5')");
        } finally {
            db.close();
        }

        Map<Integer, Answer> map = target.loadAsMap();

        assertEquals(5, map.size());
        assertEquals("A1", map.get(1).getAnswer());
        assertEquals("A2", map.get(2).getAnswer());
        assertEquals("A3", map.get(3).getAnswer());
        assertEquals("A4", map.get(4).getAnswer());
        assertEquals("A5", map.get(5).getAnswer());
    }
}
